package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.CalendarField;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JToggleButton;

/**
 * Date search.
 *
 * @author dimitri
 */
public class DateSearch extends JPanel implements FieldEditor<DateField> {
    private JComboBox combo;
    private DateField proto;
    private CalendarField date1;
    private CalendarField date2;
    private JSpinner count1;
    private JSpinner count2;
    private JToggleButton btnByDate;
    private JToggleButton btnByCount;
    private JPanel mainPanel;

    public DateSearch() {
        initComponents();
        reset();
    }

    private void initComponents() {
        setLayout(new BorderLayout(5, 5));
        combo = new JComboBox(new Integer[] {
            DateField.EXACT, DateField.INTERVAL
        });
        combo.setRenderer(new MethodRenderer());
        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        ButtonGroup gr = new ButtonGroup();
        btnByDate = new JToggleButton(UIUtils.getIcon16x16("date.png"));
        btnByCount = new JToggleButton(UIUtils.getIcon16x16("text_list_numbers.png"));
        btnByDate.setToolTipText("ძებნა თარიღით");
        btnByCount.setToolTipText("ძებნა დღეიდან");
        btnByDate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        btnByCount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        gr.add(btnByDate);
        gr.add(btnByCount);
        JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        jPanel1.add(combo);
        jPanel1.add(btnByDate);
        jPanel1.add(btnByCount);
        btnByDate.setSelected(true);
        add(jPanel1, BorderLayout.WEST);
        mainPanel = createMainPanel();
        add(mainPanel, BorderLayout.CENTER);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new CardLayout());
        panel.add(createDatePanel(), "date");
        panel.add(createCountPanel(), "count");
        return panel;
    }

    private Component createDatePanel() {
        JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        date1 = new CalendarField();
        date2 = new CalendarField();
        UIUtils.setPreferredWidth(date1, 100);
        UIUtils.setPreferredWidth(date2, 100);
        panel1.add(date1);
        panel1.add(date2);
        return panel1;
    }

    private Component createCountPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        count1 = new JSpinner();
        count2 = new JSpinner();
        UIUtils.setPreferredWidth(count1, 100);
        UIUtils.setPreferredWidth(count2, 100);
        panel.add(count1);
        panel.add(count2);
        return panel;
    }

    private void reset() {
        boolean date = btnByDate.isSelected();
        int method = (Integer) combo.getSelectedItem();
        boolean intervalVisible = method == DateField.INTERVAL;
        date2.setVisible(intervalVisible);
        count2.setVisible(intervalVisible);
        CardLayout layout = (CardLayout) mainPanel.getLayout();
        layout.show(mainPanel, date ? "date" : "count");
    }

    public DateField getField() {
        DateField field = new DateField(proto.getName(), proto.getHumanName());
        field.setMethod((Integer) combo.getSelectedItem());
        field.setSubMethod(btnByDate.isSelected() ? DateField.BY_DATE : DateField.BY_COUNT);
        if (field.getSubMethod() == DateField.BY_DATE) {
            field.setDate1(date1.getDate());
            field.setDate2(date2.getDate());
        } else {
            field.setCount1(((Number) count1.getValue()).intValue());
            field.setCount2(((Number) count2.getValue()).intValue());
        }
        return field;
    }

    public void setField(DateField field) {
        this.proto = field;
    }

    private class MethodRenderer extends DefaultListCellRenderer {

        MethodRenderer() {
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Integer) {
                int method = (Integer) value;
                if (method == DateField.EXACT) {
                    setText("ზუსტად");
                } else if (method == DateField.INTERVAL) {
                    setText("ინტერვალი");
                }
            }
            return this;
        }

    }

}
